/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import bean.Curso;
import bean.Professor;
import controlador.ControladorCurso;
import controlador.ControladorProfessor;

/**
 * 
 * @author Júnior
 */
public class PainelProfessor extends javax.swing.JPanel {

	/**
	 * Creates new form PainelProfessor
	 */
	DefaultTableModel tabelaProfessor = new DefaultTableModel(new String[] {
			"Matrícula", "Nome", "Carga Horária", "Formação" }, 0);

	public PainelProfessor() {

		initComponents();
		listagem();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jPanelBotoes = new javax.swing.JPanel();
		jButtonSalvar = new javax.swing.JButton();
		jButtonExcluir = new javax.swing.JButton();
		jButtonBuscar = new javax.swing.JButton();
		jButtonCancelar = new javax.swing.JButton();
		jButtonListar = new javax.swing.JButton();
		jButtonEditar = new javax.swing.JButton();
		jButtonAtualizar = new javax.swing.JButton();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		jComboBoxFormação = new javax.swing.JComboBox();
		jComboBoxCarga = new javax.swing.JComboBox();
		jTextFieldNome = new javax.swing.JTextField();
		jTextFieldMatricula = new javax.swing.JTextField();
		jScrollPane1 = new javax.swing.JScrollPane();
		jTable1 = new javax.swing.JTable();

		setMaximumSize(new java.awt.Dimension(830, 512));
		setMinimumSize(new java.awt.Dimension(830, 512));

		jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
		jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		jLabel1.setText("Professor");

		jPanelBotoes.setLayout(new java.awt.GridBagLayout());

		jButtonSalvar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
		jButtonSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/Imagens/Add.png"))); // NOI18N
		jButtonSalvar.setText("Salvar");
		jButtonSalvar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButtonSalvarActionPerformed(evt);
			}
		});
		jPanelBotoes.add(jButtonSalvar, new java.awt.GridBagConstraints());

		jButtonExcluir.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
		jButtonExcluir.setIcon(new javax.swing.ImageIcon(getClass()
				.getResource("/Imagens/Delete.png"))); // NOI18N
		jButtonExcluir.setText("Excluir");
		jButtonExcluir.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButtonExcluirActionPerformed(evt);
			}
		});
		jPanelBotoes.add(jButtonExcluir, new java.awt.GridBagConstraints());

		jButtonBuscar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
		jButtonBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/Imagens/Find.png"))); // NOI18N
		jButtonBuscar.setText("Buscar");
		jButtonBuscar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButtonBuscarActionPerformed(evt);
			}
		});
		jPanelBotoes.add(jButtonBuscar, new java.awt.GridBagConstraints());

		jButtonCancelar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
		jButtonCancelar.setIcon(new javax.swing.ImageIcon(getClass()
				.getResource("/Imagens/Abort.png"))); // NOI18N
		jButtonCancelar.setText("Cancelar");
		jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButtonCancelarActionPerformed(evt);
			}
		});
		jPanelBotoes.add(jButtonCancelar, new java.awt.GridBagConstraints());

		jButtonListar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
		jButtonListar.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/Imagens/List.png"))); // NOI18N
		jButtonListar.setText("Listar");
		jButtonListar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButtonListarActionPerformed(evt);
			}
		});
		jPanelBotoes.add(jButtonListar, new java.awt.GridBagConstraints());

		/*jButtonEditar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
		jButtonEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/Imagens/Application.png"))); // NOI18N
		jButtonEditar.setText("Editar");
		jPanelBotoes.add(jButtonEditar, new java.awt.GridBagConstraints());*/

		/*jButtonAtualizar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
		jButtonAtualizar.setIcon(new javax.swing.ImageIcon(getClass()
				.getResource("/Imagens/Load.png"))); // NOI18N
		jButtonAtualizar.setText("Atualizar");
		jPanelBotoes.add(jButtonAtualizar, new java.awt.GridBagConstraints());*/

		jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
		jLabel2.setText("Matrícula :");

		jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
		jLabel3.setText("Nome :");

		jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
		jLabel4.setText("Carga Horária :");

		jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
		jLabel5.setText("Formação :");

		jComboBoxFormação.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
		jComboBoxFormação.setModel(new javax.swing.DefaultComboBoxModel(
				new String[] { "Graduação", "Mestrado", "Doutorado" }));

		jComboBoxCarga.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
		jComboBoxCarga.setModel(new javax.swing.DefaultComboBoxModel(
				new String[] { "20 horas", "40 horas" }));

		jTextFieldNome.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

		jTextFieldMatricula.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

		jTable1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
		jTable1.setModel(tabelaProfessor);
		jTable1.getTableHeader().setReorderingAllowed(false);
		jScrollPane1.setViewportView(jTable1);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING,
												false)
												.addComponent(
														jPanelBotoes,
														javax.swing.GroupLayout.Alignment.LEADING,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														jLabel1,
														javax.swing.GroupLayout.Alignment.LEADING,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														830, Short.MAX_VALUE))
								.addGap(0, 0, Short.MAX_VALUE))
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		jLabel2)
																.addGap(38, 38,
																		38)
																.addComponent(
																		jTextFieldMatricula,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		200,
																		javax.swing.GroupLayout.PREFERRED_SIZE))
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		jLabel3)
																.addGap(55, 55,
																		55)
																.addComponent(
																		jTextFieldNome,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		500,
																		javax.swing.GroupLayout.PREFERRED_SIZE))
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		jLabel4)
																.addGap(8, 8, 8)
																.addComponent(
																		jComboBoxCarga,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		200,
																		javax.swing.GroupLayout.PREFERRED_SIZE))
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		jLabel5)
																.addGap(30, 30,
																		30)
																.addComponent(
																		jComboBoxFormação,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		200,
																		javax.swing.GroupLayout.PREFERRED_SIZE)))
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE))
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.addComponent(jScrollPane1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										810,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addComponent(jLabel1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										50,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addGap(4, 4, 4)
																.addComponent(
																		jLabel2))
												.addComponent(
														jTextFieldMatricula,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(7, 7, 7)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(jLabel3)
												.addComponent(
														jTextFieldNome,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(7, 7, 7)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(jLabel4)
												.addComponent(
														jComboBoxCarga,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(7, 7, 7)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(jLabel5)
												.addComponent(
														jComboBoxFormação,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(18, 18, 18)
								.addComponent(jScrollPane1,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										252, Short.MAX_VALUE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addComponent(jPanelBotoes,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										50,
										javax.swing.GroupLayout.PREFERRED_SIZE)));
	}// </editor-fold>//GEN-END:initComponents
		// Variables declaration - do not modify//GEN-BEGIN:variables

	private void jButtonSalvarActionPerformed(java.awt.event.ActionEvent evt) {
		if (jTextFieldMatricula.getText().equals("")
				|| jTextFieldNome.getText().equals("")) {
			JOptionPane.showMessageDialog(null,
					"O preenchimento de todos os campos é obrigatório!");
		} else {
			Professor p = new Professor();
			try{
			p.setMatricula(Integer.parseInt(jTextFieldMatricula.getText()));
			p.setNome(jTextFieldNome.getText());
			}catch(NumberFormatException ex){
				JOptionPane.showMessageDialog(null, "Informe uma matrícula valida!");
				jTextFieldMatricula.setText("");
			}
			int carga = jComboBoxCarga.getSelectedIndex();
			if (carga == 0) {
				p.setCarga_horaria("20");
			} else if (carga == 1) {
				p.setCarga_horaria("40");
			} 

			int forma = jComboBoxFormação.getSelectedIndex();
			if (forma == 0) {
				p.setFormacao("Graduado");
			} else if (forma == 1) {
				p.setFormacao("Mestre");
			} else {
				p.setFormacao("Doutor");
			}
			ControladorProfessor cp = new ControladorProfessor();
			if (cp.consulta(Integer.parseInt(jTextFieldMatricula.getText())) == null) {				
				cp.salvar(p);
				JOptionPane.showMessageDialog(null,"Professor cadastrado com sucesso!");
			} else {
				int opcao = JOptionPane.showConfirmDialog(null,
						"Professor já existente, se continuar os daldos serão alterados!", "continuar!",
						JOptionPane.YES_NO_OPTION);
				if (opcao == JOptionPane.YES_OPTION) {
					try {
						cp.salvar(p);
						JOptionPane.showMessageDialog(null,"Professor cadastrado com sucesso!");
					} catch (Exception e) {
						JOptionPane.showMessageDialog(null,
								"não existe professor com este código!");
					}
				} else {
					JOptionPane.showMessageDialog(null, "Alteração cancelada!");
				}
			}	
		}
		limparCampos();
		limparTabela();
		listagem();
	}

	private void jButtonExcluirActionPerformed(java.awt.event.ActionEvent evt) {
		if (jTextFieldMatricula.getText().equals("")) {
			JOptionPane.showMessageDialog(null,
					"Insira a matrícula do professor que deseja excluir!");
		} else {
			ControladorProfessor cp = new ControladorProfessor();
			int opcao = JOptionPane.showConfirmDialog(null,
					"Tem certeza que deseja excluir?", "Excluir!",
					JOptionPane.YES_NO_OPTION);
			if (opcao == JOptionPane.YES_OPTION) {
				try {
					cp.excluir(Integer.parseInt(jTextFieldMatricula.getText()));
				} catch (Exception e) {
					JOptionPane.showMessageDialog(null,
							"Tentativa de exclusão falha, não existe professor com está matrícula!");
				}
			} else {
				JOptionPane.showMessageDialog(null, "Exclusão cancelada!");
			}

		}			
		limparCampos();
		limparTabela();
		listagem();
	}

	private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {
		limparCampos();
		limparTabela();
		listagem();
	}

	private void jButtonListarActionPerformed(java.awt.event.ActionEvent evt) {
		limparCampos();
		limparTabela();
		listagem();
	}

	private void jButtonBuscarActionPerformed(java.awt.event.ActionEvent evt) {
		if (jTextFieldMatricula.getText().equals("")) {
			JOptionPane.showMessageDialog(null,
					"Informe a matrícula do professor a ser pesquisado!");
		} else {

			ControladorProfessor cp = new ControladorProfessor();
			limparTabela();
			Professor professor = cp.consulta(Integer
					.parseInt(jTextFieldMatricula.getText()));
			if (professor == null) {
				JOptionPane.showMessageDialog(null,
						"Não existe professor com essa matricúla!");
			} else {
				tabelaProfessor.addRow(new Object[] { professor.getMatricula(),
						professor.getNome(), professor.getCarga_horaria(),
						professor.getFormacao() });
			}
		}
	}

	private javax.swing.JButton jButtonAtualizar;
	private javax.swing.JButton jButtonBuscar;
	private javax.swing.JButton jButtonCancelar;
	private javax.swing.JButton jButtonEditar;
	private javax.swing.JButton jButtonExcluir;
	private javax.swing.JButton jButtonListar;
	private javax.swing.JButton jButtonSalvar;
	private javax.swing.JComboBox jComboBoxCarga;
	private javax.swing.JComboBox jComboBoxFormação;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JPanel jPanelBotoes;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable jTable1;
	private javax.swing.JTextField jTextFieldMatricula;
	private javax.swing.JTextField jTextFieldNome;

	// End of variables declaration//GEN-END:variables
	private void limparCampos() {
		jTextFieldMatricula.setText("");
		jTextFieldNome.setText("");
	}

	private void limparTabela() {
		((DefaultTableModel) jTable1.getModel()).setNumRows(0);
		jTable1.updateUI();
	}

	private void listagem() {
		tabelaProfessor.setNumRows(0);
		ControladorProfessor cp = new ControladorProfessor();
		if (cp.listar() == null) {
			JOptionPane.showMessageDialog(null,
					"Não há cursos no banco de dados!");
		} else {

			List<Professor> lista = cp.listar();
			int t = lista.size();
			for (int i = 0; i < t; i++) {
				Professor professor = lista.get(i);
				tabelaProfessor.addRow(new Object[] { professor.getMatricula(),
						professor.getNome(), professor.getCarga_horaria(),
						professor.getFormacao() });
			}
		}
	}
}
